import axios from "axios";

import { app } from "@/main";

export const instance = axios.create({});
// instance.defaults.baseURL = 'http://localhost:3000'
//请求拦截器
instance.interceptors.request.use(
    (config) => {
        // console.log("请求拦截器",config)
        //如果是不是用户接口请求，直接返回config
        // if (config.url.startsWith("/api/user") === false) {
        //     return config;
        // }
        const token = localStorage.getItem("token");
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);
//响应拦截器
instance.interceptors.response.use(
    (res) => {
        // console.log(app.$showSessionExpiredDialog, "showSessionExpiredDialog");
        // console.log("响应拦截器", res);
        if (res.data.code === 408) {
            //登录超时
            app.$showSessionExpiredDialog();
            return Promise.reject(res.data);
        }
        return res.data;
    },
    (err) => {
        return Promise.reject(err);
    }
);
